package com.ruoyi.weixin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.weixin.entity.*;
import com.ruoyi.weixin.mapper.*;
import com.ruoyi.weixin.service.PicturesService;
import com.ruoyi.weixin.service.TabService;
import com.ruoyi.weixin.vo.PicturesVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * (Pictures)表服务实现类
 *
 * @author makejava
 * @since 2023-12-26 09:51:50
 */
@Service("picturesService")
public class PicturesServiceImpl extends ServiceImpl<PicturesMapper, Pictures> implements PicturesService {

    @Autowired
    PictureMapper pictureMapper;
    @Autowired
    TabMapper tabMapper;
    @Autowired
    FrentsMapper frentsMapper;
    @Autowired
    PicUrlMapper picUrlMapper;
    @Autowired
    PicUserMapper picUserMapper;
    @Autowired
    AccountsMapper accountsMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public int NewloadPics(Frents frents) {
        frents.setCreatTime(new Date(System.currentTimeMillis()));
        int insert = frentsMapper.insert(frents);
        Frents fre = frentsMapper.selectOne(new LambdaQueryWrapper<Frents>()
                .eq(Frents::getUserId, frents.getUserId()).eq(Frents::getCreatTime, frents.getCreatTime()).eq(Frents::getStatus, frents.getStatus()));
        PicUrl prcUrl = new PicUrl();
        prcUrl.setUserId(frents.getUserId());
        prcUrl.setFrentsId(fre.getId());
        for (String url : frents.getUrls()) {
            prcUrl.setId(null);
            prcUrl.setPicUrl(url);
            picUrlMapper.insert(prcUrl);
        }
        return 0;
    }

    @Override
    public Map<String, Object> newPrcs() {
        Map<String, Object> map = new HashMap<>();
        ArrayList<Object> columns = new ArrayList<>();
        ArrayList<Object> columnData = new ArrayList<>();
        LambdaQueryWrapper<Tab> wrapper = new LambdaQueryWrapper<>();
        List<Tab> tabList = tabMapper.selectList(wrapper.isNull(Tab::getParentId).orderByAsc(Tab::getId));
        List<String> collect = tabList.stream().map(item -> item.getName()).collect(Collectors.toList());
        columns.add(collect);

        List<String> tabList1 = null;
        for (Tab tab : tabList) {
            wrapper.clear();

            tabList1 = tabMapper.selectList(wrapper.eq(Tab::getParentId, tab.getId())).stream().map(item -> item.getName()).collect(Collectors.toList());
            if (tab.getId() == 1) {
                columns.add(tabList1);
            }
            columnData.add(tabList1);
        }

        map.put("columns",columns);
        map.put("columnData",columnData);
        return map;
    }

    @Override
    @Transactional
    public int newPicss(PicturesVo picturesvo) {
        picturesvo.setCreateTime(new Date(System.currentTimeMillis()));
        Pictures pictures = new Pictures();
        BeanUtils.copyProperties(picturesvo,pictures);
        LambdaQueryWrapper<Tab> queryWrapper = new LambdaQueryWrapper<>();
        List<String> tabList = picturesvo.getTabList();
        Tab tab=null;
        if (tabList.get(1)==null){
             tab = tabMapper.selectOne(queryWrapper.eq(Tab::getName, tabList.get(0)));
        }else {
            tab = tabMapper.selectOne(queryWrapper.eq(Tab::getName, tabList.get(1)));
        }
        pictures.setTabId(tab.getId());
        pictures.setTabName(tab.getName());
        boolean save = super.save(pictures);
        Pictures one=null;
        if (picturesvo.getStatus().equals("1")){
            LambdaQueryWrapper<Pictures> eq = new LambdaQueryWrapper<Pictures>().eq(Pictures::getTypeName, picturesvo.getTypeName()).eq(Pictures::getCreateTime, picturesvo.getCreateTime()).eq(Pictures::getStatus, picturesvo.getStatus());
             one = super.getOne(eq);
            List<Integer> users = picturesvo.getUsers().stream().map(item->item.getId()).collect(Collectors.toList());
            users.add(picturesvo.getUserId());
            PicUser picUser = new PicUser();
            picUser.setPicturesId(one.getId());
            for (Integer userid : users) {
                picUser.setId(null);
                picUser.setUserId(userid);
                picUserMapper.insert(picUser);
            }
        }
        return 0;
    }

    @Override
    public List<Pictures> getPics(String userId) {
        List<Pictures> pc=pictureMapper.getPics(userId);
        return pc;
    }

    @Override
    public int newPi(Picture picture,String userId) {
        Pictures one = super.getOne(new LambdaQueryWrapper<Pictures>().eq(Pictures::getTypeName, picture.getTabName()));
        picture.setCreateTime(new Date(System.currentTimeMillis()));
        picture.setLajiId(1);
        picture.setPId(one.getId());
        List<String> picList = picture.getPicList();
        for (String url : picList) {
            picture.setId(null);
            picture.setPicUrl(url);
            pictureMapper.insert(picture);
        }
        return one.getId();
    }

    @Override
    public int checkPicName(String nickName) {
        Pictures one = super.getOne(new LambdaQueryWrapper<Pictures>().eq(Pictures::getTypeName, nickName));
        if (one==null){
            return 0;
        }else {
            return 1;
        }

    }

    @Override
    public Map<String, Object> accountcount(String userId) {
        HashMap<String, Object> map = new HashMap<>();
        int pics = super.count(new LambdaQueryWrapper<Pictures>().eq(Pictures::getUserId, userId));
        Integer pic=pictureMapper.picCount(userId);
        Integer accDay=pictureMapper.acDayCount(userId);
        Integer fre = frentsMapper.selectCount(new LambdaQueryWrapper<Frents>().eq(Frents::getUserId, userId));
        Integer account = accountsMapper.selectCount(new LambdaQueryWrapper<Account>().eq(Account::getUserId, userId));
        Integer users = userMapper.selectCount(new LambdaQueryWrapper<User>());
        map.put("pics",pics);
        map.put("accDay",accDay);
        map.put("pic",pic);
        map.put("fre",fre);
        map.put("account",account);
        map.put("user",users);
        return map;
    }
}
